package com.baseweb.webadmin.extshiro.factoryBean;

import com.baseweb.webadmin.core.model.mapper.PublicPermissionMapper;
import com.baseweb.webadmin.core.model.pojo.PublicPermission;
import com.baseweb.webadmin.core.model.pojo.PublicPermissionExample;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.mgt.FilterChainManager;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;
import org.apache.shiro.web.servlet.AbstractShiroFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class AdShiroFilterFactoryBean extends ShiroFilterFactoryBean {
    private static transient final Logger log = LoggerFactory.getLogger(AdShiroFilterFactoryBean.class);

    private SecurityManager securityManager;

    private Map<String, Filter> filters;

    private String loginUrl;
    private String successUrl;
    private String unauthorizedUrl;

    private Map<String, String> filterChainDefinitionMap;

    @Autowired
    private PublicPermissionMapper publicPermissionMapper;

    public Map<String, String> getFilterChainDefinitionMap() {
        return filterChainDefinitionMap;
    }

    public void setFilterChainDefinitionMap(Map<String, String> filterChainDefinitionMap) {
        this.filterChainDefinitionMap = filterChainDefinitionMap;
    }

    public SecurityManager getSecurityManager() {
        return securityManager;
    }

    public void setSecurityManager(SecurityManager securityManager) {
        this.securityManager = securityManager;
    }

    public String getUnauthorizedUrl() {
        return unauthorizedUrl;
    }

    public void setUnauthorizedUrl(String unauthorizedUrl) {
        this.unauthorizedUrl = unauthorizedUrl;
    }

    public String getSuccessUrl() {
        return successUrl;
    }

    public void setSuccessUrl(String successUrl) {
        this.successUrl = successUrl;
    }

    public String getLoginUrl() {
        return loginUrl;
    }

    public void setLoginUrl(String loginUrl) {
        this.loginUrl = loginUrl;
    }

    public Map<String, Filter> getFilters() {
        return filters;
    }

    public void setFilters(Map<String, Filter> filters) {
        this.filters = filters;
    }

    public void setFilterChainDefinitions(String definitions) {
        PublicPermissionExample example = new PublicPermissionExample();
        PublicPermissionExample.Criteria criteria = example.createCriteria();
        criteria.andSystemEqualTo("captcha");
        example.or(example.createCriteria().andSystemIsNull());
        List<PublicPermission> list = publicPermissionMapper.selectByExample(example);

        Map<String, String> section = new LinkedHashMap<String, String>();
        for (PublicPermission permission : list) {
            section.put(permission.getResource(), permission.getFilter());
        }

        section.put("/**", "rescFilter");
        setFilterChainDefinitionMap(section);
    }

    /**
     * 刷新filterChain
     */
    public void refreshFilter() {
        this.setFilterChainDefinitions("");
        try {
            AbstractShiroFilter filter = (AbstractShiroFilter) this.getObject();
            FilterChainManager manager = createFilterChainManager();
            PathMatchingFilterChainResolver chainResolver = new PathMatchingFilterChainResolver();
            chainResolver.setFilterChainManager(manager);
            filter.setFilterChainResolver(chainResolver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
